package github.sf.fw.utils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具
 *
 * @author zhoup
 */
public class TimeUtil {
    public static Date toDate(long timestamp) {
        return new Date(timestamp);
    }

    public static String format(long timestamp) {
        Date date = toDate(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String format(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static Date localToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+08:00")));
    }

    /**
     * 将{@code milli}(13位)毫秒级的时间戳加上{@code seconds}秒
     *
     * @param milli   毫秒级时间戳
     * @param seconds 待加的秒
     * @return 加完之后的时间戳
     */
    public static long addSeconds(long milli, long seconds) {
        Instant instant = Instant.ofEpochMilli(milli).plusSeconds(seconds);
        return instant.toEpochMilli();
    }

    /**
     * 返回字符串类型时间（ISO8601标准时间）
     */
    public static String getIso8601(Instant timestamp) {
        LocalDateTime ldt = LocalDateTime.ofInstant(timestamp, ZoneId.of("Asia/Shanghai"));
        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime date = OffsetDateTime.of(ldt, offset);
//        DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
        return date.format(iso8601Formatter);
    }

    /**
     * 解析iso8601格式的日期
     *
     * @param iso8601 字符串
     */
    public static LocalDateTime parseIso8601(String iso8601) {
//        DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.XXX");
        return LocalDateTime.parse(iso8601, iso8601Formatter);
    }

    /**
     * localDateTime转为时间戳
     */
    public static long toTimestamp(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+08:00"));
    }

    public static String timeStampToRfc3339(long timeStamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return simpleDateFormat.format(new Date(timeStamp));
    }

    public static LocalDateTime from(Date date) {
        Instant instant = date.toInstant();
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.of("+08:00"));
        return offsetDateTime.toLocalDateTime();
    }

    enum TimeFormat {
        RFC3339("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
        ISO8602("yyyy-MM-dd'T'HH:mm:ssXXX");
        final String pattern;

        TimeFormat(String pattern) {
            this.pattern = pattern;
        }

        public String getPattern() {
            return pattern;
        }
    }
}
